package com.zking.controller.dwm;

import com.zking.entity.User;
import com.zking.service.dwm.UserService_dwm;
import com.zking.utils.EncryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping(value = "/dwm1")
public class LoginController_dwm {

    @Autowired
    private UserService_dwm userService_dwm;

    @RequestMapping(value = "/login")
    @ResponseBody
    //用户登录
    public  String login(String username, String password, HttpSession httpSession){
        User login_user = userService_dwm.queryUser(username);
        if (login_user !=null){
            if (login_user.getPassword().equals(EncryptUtils.getEncryptStr(password))){
                httpSession.setAttribute("user",login_user);
                return "true";
            }
            return "false";
        }
        return "false";
    }
    @RequestMapping(value = "/tolog")
    public String tolog(){
        return "dwm/login";
    }
    @RequestMapping(value = "/in")
    public String index(){
        return "dwm/index";
    }
    @RequestMapping(value = "/register", name = "注册")
    public String toIndex1(){
        return "dwm/register";
    }

    @RequestMapping(value = "/cart")
    public String toCart(){
        return "dwm/cart";
    }

    //用户注册
    @RequestMapping(value = "/register2")
    @ResponseBody
    public int register(String username,String password,String phone){
        //返回0代表用户已存在，1代表成功
        System.out.println(username+"  "+password+"  "+phone);
        User user = userService_dwm.queryByname(username);
        if (user != null){
            return 0;
        }
        int register = userService_dwm.register(username, password, phone);
        System.out.println(register);;
        return 1;
    }


}
